<?php
require_once ('../lib/config.php');
require_once ('../lib/facebook.php');
require_once ('../lib/friends.php');
require_once ('../lib/friend.php');
require_once ('../lib/test.php');
require_once ('../../simpletest/autorun.php');
require_once ('../lib/init.php');
require_once ('../data_sample/mockup_data_message.php');

class TestFunc_processMessage extends UnitTestCase {
    
    function setUp(){
        global $login_button,$facebook;
        
        //if there is a session, skip code for processting sesseion
        if(isLoggedIn()){
            try{
                $me = getOneself();
            }catch(Exception $e){
                print($login_button);
                exit();
            }
        }
    }
    
    //I SENT CASES
    //Case1: I sent a message to a friend and got no reply
    function test_case1(){
        global $messageCase1;
        $friends = array();
        $data = $messageCase1; 
        
        processMessage($friends, getResultSet("threadQuery", $data), getResultSet("messageQuery", $data));

        $this->assertNotNull($friends);
        $this->assertTrue(is_array($friends));
        $this->assertFalse(empty($friends));

        //there must be one friend
        $this->assertTrue(count($friends) == 1);
        
        //And the friend must have One one-way message
        $friend = array_pop($friends);
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 1);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 0);
    }
    
    //Case2: I sent a message to friends and get no reply
    function test_case2(){
        global $messageCase2;
        $friends = array();
        $data = $messageCase2; 
        
        processMessage($friends, getResultSet("threadQuery", $data), getResultSet("messageQuery", $data));

        $this->assertNotNull($friends);
        $this->assertTrue(is_array($friends));
        $this->assertFalse(empty($friends));

        //there must be TWO friend
        $this->assertTrue(count($friends) == 2);
        
        //And the friends must have One one-way message
        $friend = array_pop($friends);
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 1);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 0);
        
        $friend = array_pop($friends);
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 1);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 0);
    }
    //Case3: I sent a message to a friend and got a reply
    function test_case3(){
        global $messageCase3;
        $friends = array();
        $data = $messageCase3; 
        
        processMessage($friends, getResultSet("threadQuery", $data), getResultSet("messageQuery", $data));

        $this->assertNotNull($friends);
        $this->assertTrue(is_array($friends));
        $this->assertFalse(empty($friends));

        //there must be ONE friend
        $this->assertTrue(count($friends) == 1);
        
        //And the friend must have One two-way message
        $friend = array_pop($friends);
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 0);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 1);
    }
    //Case4: I sent a messgae to friends and got a reply from one of them
    function test_case4(){
        global $messageCase4;
        $friends = array();
        $data = $messageCase4; 
        
        processMessage($friends, getResultSet("threadQuery", $data), getResultSet("messageQuery", $data));

        $this->assertNotNull($friends);
        $this->assertTrue(is_array($friends));
        $this->assertFalse(empty($friends));

        //there must be ONE friend
        $this->assertTrue(count($friends) == 2);
        
        //And the friend must have One two-way message 
        $friend = $friends['100000525262538'];
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 1);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 0);
        
        $friend = $friends['100000416787478'];
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 0);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 1);
    }
    //Case5: I sent a message to friends and got replies from all
    function test_case5(){
        global $messageCase5;
        $friends = array();
        $data = $messageCase5; 
        
        processMessage($friends, getResultSet("threadQuery", $data), getResultSet("messageQuery", $data));

        $this->assertNotNull($friends);
        $this->assertTrue(is_array($friends));
        $this->assertFalse(empty($friends));

        //there must be TWO friend
        $this->assertTrue(count($friends) == 2);
        
        //And the friend must have One two-way message 
        $friend = $friends['100000525262538'];
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 0);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 1);
        
        $friend = $friends['100000416787478'];
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 0);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 1);
    }
    
    //Friend SENT CASES
    //Case6: A friend sent me a message and I didn't reply
    function test_case6(){
        global $messageCase6;
        $friends = array();
        $data = $messageCase6; 
        
        processMessage($friends, getResultSet("threadQuery", $data), getResultSet("messageQuery", $data));

        $this->assertNotNull($friends);
        $this->assertTrue(is_array($friends));
        $this->assertFalse(empty($friends));

        //there must be ONE friend
        $this->assertTrue(count($friends) == 1);
        
        //And the friend must have One two-way message 
        $friend = $friends['100000416787478'];
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 1);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 0);
        
    }
    //Case7: A friend sent me a message and I replied
    function test_case7(){
        global $messageCase7;
        $friends = array();
        $data = $messageCase7; 
        
        processMessage($friends, getResultSet("threadQuery", $data), getResultSet("messageQuery", $data));

        $this->assertNotNull($friends);
        $this->assertTrue(is_array($friends));
        $this->assertFalse(empty($friends));

        //there must be ONE friend
        $this->assertTrue(count($friends) == 1);
        
        //And the friend must have One two-way message 
        $friend = $friends['100000416787478'];
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 0);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 1);
    }
    //Case8: A friend sent me a message and I replied
    function test_case8(){
        global $messageCase8;
        $friends = array();
        $data = $messageCase8; 
        
        processMessage($friends, getResultSet("threadQuery", $data), getResultSet("messageQuery", $data));

        $this->assertNotNull($friends);
        $this->assertTrue(is_array($friends));
        $this->assertFalse(empty($friends));

        //there must be ONE friend
        $this->assertTrue(count($friends) == 1);
        
        //And the friend must have One two-way message 
        $friend = $friends['100000416787478'];
        $this->assertTrue($friend->getNumberOfData("ONEWAY_MESSAGE_FLAG") == 1);
        $this->assertTrue($friend->getNumberOfData("TWOWAY_MESSAGE_FLAG") == 1);
    }
    
    function test_real_data(){
        $data = getMessage();
        
        processMessage($friends, getResultSet("threadQuery", $data), getResultSet("messageQuery", $data));

        $this->assertNotNull($friends);
        $this->assertTrue(is_array($friends));
        $this->assertFalse(empty($friends));
        
        $this->assertTrue(count($friends) > 0);
    }
}
?>
